#!/usr/bin/env bash

setup_env() {
    mamba create -yn legate -c ~/.artifacts/conda-build/legate_core -c ~/.artifacts/conda-build/cunumeric -c conda-forge -c "nvidia/label/cuda-12.0.0" legate-core cunumeric
}

setup_test_env() {
    mamba install -y "clang-tools>=8" "clang>=8" colorama coverage mock pre-commit pytest-cov pytest-lazy-fixture pytest-mock pytest types-docutils pynvml

    pip install tifffile
}

setup_docs_env() {
    mamba install -y pandoc doxygen
    pip install ipython jinja2 "markdown<3.4.0" "pydata-sphinx-theme>=0.13"  myst-parser  nbsphinx  sphinx-copybutton  "sphinx>=4.4.0"

}

setup_mypy_env() {
    mamba install -y "mypy>=0.961" jinja2 nbsphinx sphinx-copybutton  "sphinx>=4.4.0" types-docutils
}

test-cunumeric() {
    set -xeo pipefail

    setup_env;

    set +u
    mamba activate legate;
    conda info;

    cd ~/cunumeric;

    case "$1" in
        "test")
            echo "Executing tests..."
            shift;
            setup_test_env;
            ./test.py --verbose "$@"
            ;;
        "mypy")
            echo "Installing and executing mypy..."
            shift;
            setup_mypy_env;
            mypy cunumeric
            ;;
        "docs")
            echo "Building docs..."
            shift;
            setup_docs_env;
            cd docs/cunumeric
            make clean html
            ;;
        *)
            echo "Invalid command: $1"
            return 1
            ;;
    esac
}

(test-cunumeric "$@");